package com.bzsg.rightsManagementSystem.service.impl

import com.bzsg.rightsManagementSystem.dao.UserDao
import com.bzsg.rightsManagementSystem.dao.UserRoleRelationDao
import com.bzsg.rightsManagementSystem.pojo.PersonUser
import com.bzsg.rightsManagementSystem.service.UserService
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service


@Service
class UserServiceImpl : UserService {
    private val logger = LoggerFactory.getLogger(this.javaClass)

    @Autowired
    private lateinit var userDao: UserDao

    @Autowired
    private lateinit var userRoleRelationDao: UserRoleRelationDao

    override fun queryById(id: Int): PersonUser? {
        return userDao.queryById(id)
    }

    override fun queryAllByLimit(offset: Int, limit: Int): List<PersonUser>? {
        val start = (offset - 1) * limit
        return userDao.queryAllByLimit(start, limit)
    }

    override fun queryAll(): List<PersonUser>? {
        return userDao.queryAll()
    }

    override fun insert(personUser: PersonUser) {
        userDao.insert(personUser)
    }

    override fun update(personUser: PersonUser) {
        logger.info(personUser.toString())
        userDao.update(personUser)
    }

    override fun deleteById(id: Int) {
        userDao.deleteById(id)
    }

    override fun selectUserByName(userName: String): PersonUser? {
        return userDao.selectUserByName(userName)
    }

    override fun selectUserByAccount(account: String): PersonUser? {
        return userDao.selectUserByAccount(account)
    }

    override fun selectTotalUser(): Int {
        return userDao.selectTotalUser()
    }

    override fun deleteAllUserRoleRelationByUserId(userId: Int) {
        userRoleRelationDao.deleteAllUserRoleRelationByUserId(userId)
    }

    override fun addUserRoleRelation(userId: Int, roleId: Int) {
        userRoleRelationDao.addUserRoleRelation(userId, roleId)
    }
}